<script setup lang="ts">
import Account from './account.vue'
import Phone from './phone.vue'
import { localCache } from '@/utils/cache'

// ?? 用于设置默认值，当前面的值为null or undefined时
const isKeepPassword = ref<boolean>(localCache.getCache('isKeepPassword') ?? false)
watch(isKeepPassword, (newValue) => {
  localCache.setCache('isKeepPassword', newValue)
})
const activeName = ref('account')
// 通过ref获取子组件元素；注意组件类型注解的写法，类型为Account组件的实例；这是固定写法
const accountRef = ref<InstanceType<typeof Account>>()

const handleLoginClick = () => {
  if (activeName.value === 'account') {
    // 上面定义的accountRef由于初始值为undefine，所以这里需要使用可选链
    accountRef.value?.loginAction(isKeepPassword.value)
  } else {
    console.log('phone')
  }
}
</script>

<template>
  <div class="login-panel">
    <h1 class="title">后台管理系统</h1>

    <div class="tabs">
      <el-tabs type="border-card" stretch v-model="activeName">
        <el-tab-pane label="帐号登录" name="account">
          <template #label>
            <div class="label">
              <el-icon><UserFilled /></el-icon>
              <span class="text">帐号登录</span>
            </div>
          </template>
          <Account ref="accountRef" />
        </el-tab-pane>

        <el-tab-pane label="手机登录" name="phone">
          <template #label>
            <div class="label">
              <el-icon><Cellphone /></el-icon>
              <span class="text">手机登录</span>
            </div>
          </template>
          <Phone />
        </el-tab-pane>
      </el-tabs>
    </div>

    <div class="account-control">
      <el-checkbox v-model="isKeepPassword">记住密码</el-checkbox>
      <el-link type="primary">忘记密码</el-link>
    </div>

    <el-button type="primary" class="login-btn" @click="handleLoginClick" size="large">立即登录</el-button>
  </div>
</template>

<style lang="less" scoped>
.login-panel {
  margin-bottom: 150px;
  width: 330px;

  .title {
    text-align: center;
    margin-bottom: 15px;
  }

  .label {
    display: flex;
    align-items: center;
    justify-content: center;
    .text {
      margin-left: 5px;
    }
  }

  .account-control {
    margin-top: 10px;
    display: flex;
    justify-content: space-between;
  }

  .login-btn {
    width: 100%;
    margin-top: 10px;
    // --el-button-size: 50px;
  }
}
</style>
